package xt.outfood.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author 小小爱欧
 * @date 2022/7/12 15:40
 * 发送附件，图片等，多部邮件,较高级邮件
 */
//public class SendMailServiceAdviceImpl  implements SendMailService{
public class SendMailServiceAdviceImpl  {
    @Autowired
    private JavaMailSender javaMailSender;
    //发送人
    private String from="";
    //接收人
    private String[] to={"",""};
    //标题
    private String subject="标题";
    //正文
    private String context="<a href='https://www.baidu.com'>点开惊喜</a>";

    public void sendMail(String email,String code) throws MessagingException {
        //创建复杂邮件
        MimeMessage message=javaMailSender.createMimeMessage();
        //MimeMessageHelper参数添加true指允许发送附件
        MimeMessageHelper helper=new MimeMessageHelper(message,true);
        helper.setFrom(String.valueOf(to));
        helper.setTo(from);
        helper.setSubject(subject);
        //setText()解析html，需把后面的参数改成true
        helper.setText(context,true);
        File file1=new File("文件路径");
        File file2=new File("文件路径");
        helper.addAttachment("文件标题.png",file1);

        javaMailSender.send(message);

    }
}
